home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 3.iso / dist / fw_elisp-intro.idb / usr / freeware / info / emacs-lisp-intro.info-4.z / emacs-lisp-intro.info-4
Encoding:
GNU Info File  |  1998-10-28  |  49.4 KB  |  1,223 lines

  1. This is Info file emacs-lisp-intro.info, produced by Makeinfo version
  2. 1.67 from the input file emacs-lisp-intro.texi.
  3.  
  4.    This is an introduction to `Programming in Emacs Lisp', for people
  5. who are not programmers.
  6.  
  7.    Edition 1.05, 21 October 1997
  8.  
  9.    Copyright (C) 1990, '91, '92, '93, '94, '95, '97 Free Software
  10. Foundation, Inc.
  11.  
  12.    Permission is granted to make and distribute verbatim copies of this
  13. manual provided the copyright notice and this permission notice are
  14. preserved on all copies.
  15.  
  16.    Permission is granted to copy and distribute modified versions of
  17. this manual under the conditions for verbatim copying, provided also
  18. that the sections entitled "Copying" and "GNU General Public License"
  19. are included exactly as in the original, and provided that the entire
  20. resulting derived work is distributed under the terms of a permission
  21. notice identical to this one.
  22.  
  23.    Permission is granted to copy and distribute translations of this
  24. manual into another language, under the above conditions for modified
  25. versions, except that this permission notice may be stated in a
  26. translation approved by the Free Software Foundation.
  27.  
  28. 
  29. File: emacs-lisp-intro.info,  Node: Review,  Next: defun Exercises,  Prev: save-excursion,  Up: Writing Defuns
  30.  
  31. Review
  32. ======
  33.  
  34.    In the last few chapters we have introduced a fair number of
  35. functions and special forms.  Here they are described in brief, along
  36. with a few similar functions that have not been mentioned yet.
  37.  
  38. `eval-last-sexp'
  39.      Evaluate the last symbolic expression before the current location
  40.      of point.  The value is printed in the echo area unless the
  41.      function is invoked with an argument; in that case, the output is
  42.      printed in the current buffer.  This command is normally bound to
  43.      `C-x C-e'.
  44.  
  45. `defun'
  46.      Define function.  This special form has up to five parts: the name,
  47.      a template for the arguments that will be passed to the function,
  48.      documentation, an optional interactive declaration, and the body
  49.      of the definition.
  50.  
  51.      For example:
  52.  
  53.           (defun back-to-indentation ()
  54.             "Point to first visible character on line."
  55.             (interactive)
  56.             (beginning-of-line 1)
  57.             (skip-chars-forward " \t"))
  58.  
  59. `interactive'
  60.      Declare to the interpreter that the function can be used
  61.      interactively.  This special form may be followed by a string with
  62.      one or more parts that pass the information to the arguments of the
  63.      function, in sequence.  These parts may also tell the interpreter
  64.      to prompt for information.  Parts of the string are separated by
  65.      newlines, `\n'.
  66.  
  67.      Common code characters are:
  68.  
  69.     `b'
  70.           The name of an existing buffer.
  71.  
  72.     `f'
  73.           The name of an existing file.
  74.  
  75.     `p'
  76.           The numeric prefix argument.  (Note that this `p' is lower
  77.           case.)
  78.  
  79.     `r'
  80.           Point and the mark, as two numeric arguments, smallest first.
  81.           This is the only code letter that specifies two successive
  82.           arguments rather than one.
  83.  
  84.      *Note Code Characters for `interactive': (elisp)Interactive Codes,
  85.      for a complete list of code characters.
  86.  
  87. `let'
  88.      Declare that a list of variables is for use within the body of the
  89.      `let' and give them an initial value, either `nil' or a specified
  90.      value; then evaluate the rest of the expressions in the body of
  91.      the `let' and return the value of the last one.  Inside the body
  92.      of the `let', the Lisp interpreter does not see the values of the
  93.      variables of the same names that are bound outside of the `let'.
  94.  
  95.      For example,
  96.  
  97.           (let ((foo (buffer-name))
  98.                 (bar (buffer-size)))
  99.             (message
  100.              "This buffer is %s and has %d characters."
  101.              foo bar))
  102.  
  103. `save-excursion'
  104.      Record the values of point and mark and the current buffer before
  105.      evaluating the body of this special form.  Restore the values of
  106.      point and mark and buffer afterward.
  107.  
  108.      For example,
  109.  
  110.           (message "We are %d characters into this buffer."
  111.                    (- (point)
  112.                       (save-excursion
  113.                         (goto-char (point-min)) (point))))
  114.  
  115. `if'
  116.      Evaluate the first argument to the function; if it is true,
  117.      evaluate the second argument; else evaluate the third argument, if
  118.      there is one.
  119.  
  120.      The `if' special form is called a "conditional".  There are other
  121.      conditionals in Emacs Lisp, but `if' is perhaps the most commonly
  122.      used.
  123.  
  124.      For example,
  125.  
  126.           (if (string= (int-to-string 19)
  127.                        (substring (emacs-version) 10 12))
  128.               (message "This is version 19 Emacs")
  129.             (message "This is not version 19 Emacs"))
  130.  
  131. `equal'
  132. `eq'
  133.      Test whether two objects are the same.  `equal' returns true if the
  134.      two objects have a similar structure and contents.  Another
  135.      function, `eq', returns true if both arguments are actually the
  136.      same object.
  137.  
  138. `<'
  139. `>'
  140. `<='
  141. `>='
  142.      The `<' function tests whether the first argument is smaller than
  143.      the second argument.  A corresponding function, `>', tests whether
  144.      the first argument is greater than the second.  Likewise, `<='
  145.      tests whether the first argument is less than or equal to the
  146.      second and `>=' tests whether the first argument is greater than
  147.      or equal to the second.  In all cases, both arguments must be
  148.      numbers.
  149.  
  150. `message'
  151.      Print a message in the echo area.  The message can be only one line
  152.      long.  The first argument is a string that can contain `%s', `%d',
  153.      or `%c' to print the value of arguments that follow the string.
  154.      The argument used by `%s' must be a string or a symbol; the
  155.      argument used by `%d' must be a number.  The argument used by `%c'
  156.      must be a number; it will be printed as the character with that
  157.      ASCII code.
  158.  
  159. `setq'
  160. `set'
  161.      The `setq' function sets the value of its first argument to the
  162.      value of the second argument.  The first argument is automatically
  163.      quoted by `setq'.  It does the same for succeeding pairs of
  164.      arguments.  Another function, `set', takes only two arguments and
  165.      evaluates both of them before setting the value returned by its
  166.      first argument to the value returned by its second argument.
  167.  
  168. `buffer-name'
  169.      Without an argument, return the name of the buffer, as a string.
  170.  
  171. `buffer-file-name'
  172.      Without an argument, return the name of the file the buffer is
  173.      visiting.
  174.  
  175. `current-buffer'
  176.      Return the buffer in which Emacs is active; it may not be the
  177.      buffer that is visible on the screen.
  178.  
  179. `other-buffer'
  180.      Return the most recently selected buffer (other than the buffer
  181.      passed to `other-buffer' as an argument and other than the current
  182.      buffer).
  183.  
  184. `switch-to-buffer'
  185.      Select a buffer for Emacs to be active in and display it in the
  186.      current window so users can look at it.  Usually bound to `C-x b'.
  187.  
  188. `set-buffer'
  189.      Switch Emacs's attention to a buffer on which programs will run.
  190.      Don't alter what the window is showing.
  191.  
  192. `buffer-size'
  193.      Return the number of characters in the current buffer.
  194.  
  195. `point'
  196.      Return the value of the current position of the cursor, as an
  197.      integer counting the number of characters from the beginning of the
  198.      buffer.
  199.  
  200. `point-min'
  201.      Return the minimum permissible value of point in the current
  202.      buffer.  This is 1, unless narrowing is in effect.
  203.  
  204. `point-max'
  205.      Return the value of the maximum permissible value of point in the
  206.      current buffer.  This is the end of the buffer, unless narrowing
  207.      is in effect.
  208.  
  209. 
  210. File: emacs-lisp-intro.info,  Node: defun Exercises,  Prev: Review,  Up: Writing Defuns
  211.  
  212. Exercises
  213. =========
  214.  
  215.    * Write a non-interactive function that doubles the value of its
  216.      argument, a number.  Make that function interactive.
  217.  
  218.    * Write a function that tests whether the current value of
  219.      `fill-column' is greater than the argument passed to the function,
  220.      and if so, prints an appropriate message.
  221.  
  222. 
  223. File: emacs-lisp-intro.info,  Node: Buffer Walk Through,  Next: More Complex,  Prev: Writing Defuns,  Up: Top
  224.  
  225. A Few Buffer-Related Functions
  226. ******************************
  227.  
  228.    In this chapter we study in detail several of the functions used in
  229. GNU Emacs.  This is called a "walk-through".  These functions are used
  230. as examples of Lisp code, but are not imaginary examples; with the
  231. exception of the first, simplified function definition, these functions
  232. show the actual code used in GNU Emacs.  You can learn a great deal from
  233. these definitions.  The functions described here are all related to
  234. buffers.  Later, we will study other functions.
  235.  
  236. * Menu:
  237.  
  238. * Finding More::                How to find more information.
  239. * simplified-beginning-of-buffer::  Shows `goto-char',
  240.                                 `point-min', and `push-mark'.
  241. * mark-whole-buffer::           Almost the same as `beginning-of-buffer'.
  242. * append-to-buffer::            Uses `save-excursion' and
  243.                                 `insert-buffer-substring'.
  244. * Buffer Related Review::       Review.
  245. * Buffer Exercises::
  246.  
  247. 
  248. File: emacs-lisp-intro.info,  Node: Finding More,  Next: simplified-beginning-of-buffer,  Prev: Buffer Walk Through,  Up: Buffer Walk Through
  249.  
  250. Finding More Information
  251. ========================
  252.  
  253.    In this walk-through, I will describe each new function as we come to
  254. it, sometimes in detail and sometimes briefly.  If you are interested,
  255. you can get the full documentation of any Emacs Lisp function at any
  256. time by typing `C-h f' and then the name of the function (and then
  257. <RET>).  Similarly, you can get the full documentation for a variable
  258. by typing `C-h v' and then the name of the variable (and then <RET>).
  259.  
  260.    Also, if you want to see a function in its original source file, you
  261. can use the `find-tags' function to jump to it.  Type `M-.' (i.e., type
  262. the <META> and the period key at the same time, or else type the <ESC>
  263. key and then type the period key), and then, at the prompt, type in the
  264. name of the function whose source code you want to see, such as
  265. `mark-whole-buffer', and then type <RET>.  Emacs will switch buffers
  266. and display the source code for the function on your screen.  To switch
  267. back to this buffer, type `C-x b <RET>'.
  268.  
  269.    Depending on how the initial default values of your copy of Emacs are
  270. set, you may also need to specify a `tags table', which is a file
  271. called `TAGS'.  The one you will most likely want to specify is in the
  272. `emacs/src' directory; thus you would use the `M-x visit-tags-table'
  273. command and specify a pathname such as
  274. `/usr/local/lib/emacs/19.23/src/TAGS'.  *Note Tag Tables: (emacs)Tags.
  275. Also, see *Note Create Your Own `TAGS' File: etags, for how to create
  276. your own.
  277.  
  278.    After you become more familiar with Emacs Lisp, you will find that
  279. you will frequently use `find-tags' to navigate your way around source
  280. code; and you will create your own `TAGS' tables.
  281.  
  282.    Incidentally, the files that contain Lisp code are conventionally
  283. called "libraries".  The metaphor is derived from that of a specialized
  284. library, such as a law library or an engineering library, rather than a
  285. general library.  Each library, or file, contains functions that relate
  286. to a particular topic or activity, such as `abbrev.el' for handling
  287. abbreviations and other typing shortcuts, and `help.el' for on-line
  288. help.  (Sometimes several libraries provide code for a single activity,
  289. as the various `rmail...' files provide code for reading electronic
  290. mail.) In `The GNU Emacs Manual', you will see sentences such as "The
  291. `C-h p' command lets you search the standard Emacs Lisp libraries by
  292. topic keywords."
  293.  
  294. 
  295. File: emacs-lisp-intro.info,  Node: simplified-beginning-of-buffer,  Next: mark-whole-buffer,  Prev: Finding More,  Up: Buffer Walk Through
  296.  
  297. A Simplified `beginning-of-buffer' Definition
  298. =============================================
  299.  
  300.    The `beginning-of-buffer' command is a good function to start with
  301. since you are likely to be familiar with it and it is easy to
  302. understand.  Used as an interactive command, `beginning-of-buffer'
  303. moves the cursor to the beginning of the buffer, leaving the mark at the
  304. previous position.  It is generally bound to `M-<'.
  305.  
  306.    In this section, we will discuss a shortened version of the function
  307. that shows how it is most frequently used.  This shortened function
  308. works as written, but it does not contain the code for a complex option.
  309. In another section, we will describe the entire function.  (*Note
  310. Complete Definition of `beginning-of-buffer': beginning-of-buffer.)
  311.  
  312.    Before looking at the code, let's consider what the function
  313. definition has to contain: it must include an expression that makes the
  314. function interactive so it can be called by typing `M-x
  315. beginning-of-buffer' or by typing a keychord such as `C-<'; it must
  316. include code to leave a mark at the original position in the buffer;
  317. and it must include code to move the cursor to the beginning of the
  318. buffer.
  319.  
  320.    Here is the complete text of the shortened version of the function:
  321.  
  322.      (defun simplified-beginning-of-buffer ()
  323.        "Move point to the beginning of the buffer;
  324.      leave mark at previous position."
  325.        (interactive)
  326.        (push-mark)
  327.        (goto-char (point-min)))
  328.  
  329.    Like all function definitions, this definition has five parts
  330. following the special form `defun':
  331.  
  332.   1. The name: in this case, `simplified-beginning-of-buffer'.
  333.  
  334.   2. A list of the arguments: in this case, an empty list, `()',
  335.  
  336.   3. The documentation string.
  337.  
  338.   4. The interactive expression.
  339.  
  340.   5. The body.
  341.  
  342. In this function definition, the argument list is empty; this means that
  343. this function does not require any arguments.  (When we look at the
  344. definition for the complete function, we will see that it may be passed
  345. an optional argument.)
  346.  
  347.    The interactive expression tells Emacs that the function is intended
  348. to be used interactively.  In this case, `interactive' does not have an
  349. argument because `simplified-beginning-of-buffer' does not require one.
  350.  
  351.    The body of the function consists of the two lines:
  352.  
  353.      (push-mark)
  354.      (goto-char (point-min))
  355.  
  356.    The first of these lines is the expression, `(push-mark)'.  When
  357. this expression is evaluated by the Lisp interpreter, it sets a mark at
  358. the current position of the cursor, wherever that may be.  The position
  359. of this mark is saved in the mark ring.
  360.  
  361.    The next line is `(goto-char (point-min))'.  This expression jumps
  362. the cursor to the minimum point in the buffer, that is, to the
  363. beginning of the buffer (or to the beginning of the accessible portion
  364. of the buffer if it is narrowed.  *Note Narrowing and Widening:
  365. Narrowing & Widening.)
  366.  
  367.    The `push-mark' command sets a mark at the place where the cursor
  368. was located before it was moved to the beginning of the buffer by the
  369. `(goto-char (point-min))' expression.  Consequently, you can, if you
  370. wish, go back to where you were originally by typing `C-x C-x'.
  371.  
  372.    That is all there is to the function definition!
  373.  
  374.    When you are reading code such as this and come upon an unfamiliar
  375. function, such as `goto-char', you can find out what it does by using
  376. the `describe-function' command.  To use this command, type `C-h f' and
  377. then type in the name of the function and press <RET>.  The
  378. `describe-function' command will print the function's documentation
  379. string in a `*Help*' window.  For example, the documentation for
  380. `goto-char' is:
  381.  
  382.      One arg, a number.  Set point to that number.
  383.      Beginning of buffer is position (point-min),
  384.      end is (point-max).
  385.  
  386. (The prompt for `describe-function' will offer you the symbol preceding
  387. the cursor, so you can save typing by positioning the cursor right
  388. after the function and then typing `C-h f <RET>'.)
  389.  
  390.    The `end-of-buffer' function definition is written in the same way as
  391. the `beginning-of-buffer' definition except that the body of the
  392. function contains the expression `(goto-char (point-max))' in place of
  393. `(goto-char (point-min))'.
  394.  
  395. 
  396. File: emacs-lisp-intro.info,  Node: mark-whole-buffer,  Next: append-to-buffer,  Prev: simplified-beginning-of-buffer,  Up: Buffer Walk Through
  397.  
  398. The Definition of `mark-whole-buffer'
  399. =====================================
  400.  
  401.    The `mark-whole-buffer' function is no harder to understand than the
  402. `simplified-beginning-of-buffer' function.  In this case, however, we
  403. will look at the complete function, not a shortened version.
  404.  
  405.    The `mark-whole-buffer' function is not as commonly used as the
  406. `beginning-of-buffer' function, but is useful nonetheless: it marks a
  407. whole buffer as a region by putting point at the beginning and a mark
  408. at the end of the buffer.  It is generally bound to `C-x h'.
  409.  
  410.    The code for the complete function looks like this:
  411.  
  412.      (defun mark-whole-buffer ()
  413.        "Put point at beginning and mark at end of buffer."
  414.        (interactive)
  415.        (push-mark (point))
  416.        (push-mark (point-max))
  417.        (goto-char (point-min)))
  418.  
  419.    Like all other functions, the `mark-whole-buffer' function fits into
  420. the template for a function definition.  The template looks like this:
  421.  
  422.      (defun NAME-OF-FUNCTION (ARGUMENT-LIST)
  423.        "DOCUMENTATION..."
  424.        (INTERACTIVE-EXPRESSION...)
  425.        BODY...)
  426.  
  427.    Here is how the function works: the name of the function is
  428. `mark-whole-buffer'; it is followed by an empty argument list, `()',
  429. which means that the function does not require arguments.  The
  430. documentation comes next.
  431.  
  432.    The next line is an `(interactive)' expression that tells Emacs that
  433. the function will be used interactively.  These details are similar to
  434. the `simplified-beginning-of-buffer' function described in the previous
  435. section.
  436.  
  437. * Menu:
  438.  
  439. * Body of mark-whole-buffer::   Only three lines of code.
  440.  
  441. 
  442. File: emacs-lisp-intro.info,  Node: Body of mark-whole-buffer,  Prev: mark-whole-buffer,  Up: mark-whole-buffer
  443.  
  444. Body of `mark-whole-buffer'
  445. ---------------------------
  446.  
  447.    The body of the `mark-whole-buffer' function consists of three lines
  448. of code:
  449.  
  450.      (push-mark (point))
  451.      (push-mark (point-max))
  452.      (goto-char (point-min))
  453.  
  454.    The first of these lines is the expression, `(push-mark (point))'.
  455.  
  456.    This line does exactly the same job as the first line of the body of
  457. the `simplified-beginning-of-buffer' function, which is written
  458. `(push-mark)'.  In both cases, the Lisp interpreter sets a mark at the
  459. current position of the cursor.
  460.  
  461.    I don't know why the expression in `mark-whole-buffer' is written
  462. `(push-mark (point))' and the expression in `beginning-of-buffer' is
  463. written `(push-mark)'.  Perhaps whoever wrote the code did not know
  464. that the argument for `push-mark' is optional and that if `push-mark'
  465. is not passed an argument, the function automatically sets mark at the
  466. location of point by default.  Or perhaps the expression was written so
  467. as to parallel the structure of the next line.  In any case, the line
  468. causes Emacs to determine the position of point and set a mark there.
  469.  
  470.    The next line of `mark-whole-buffer' is `(push-mark (point-max))'.
  471. This expression sets a mark at the point in the buffer that has the
  472. highest number.  This will be the end of the buffer (or, if the buffer
  473. is narrowed, the end of the accessible portion of the buffer.  *Note
  474. Narrowing and Widening: Narrowing & Widening, for more about
  475. narrowing.)  After this mark has been set, the previous mark, the one
  476. set at point, is no longer set, but Emacs remembers its position, just
  477. as all other recent marks are always remembered.  This means that you
  478. can, if you wish, go back to that position by typing `C-u C-<SPC>'
  479. twice.
  480.  
  481.    Finally, the last line of the function is `(goto-char
  482. (point-min)))'.  This is written exactly the same way as it is written
  483. in `beginning-of-buffer'.  The expression moves the cursor to the
  484. minimum point in the buffer, that is, to the beginning of the buffer
  485. (or to the beginning of the accessible portion of the buffer).  As a
  486. result of this, point is placed at the beginning of the buffer and mark
  487. is set at the end of the buffer.  The whole buffer is, therefore, the
  488. region.
  489.  
  490. 
  491. File: emacs-lisp-intro.info,  Node: append-to-buffer,  Next: Buffer Related Review,  Prev: mark-whole-buffer,  Up: Buffer Walk Through
  492.  
  493. The Definition of `append-to-buffer'
  494. ====================================
  495.  
  496.    The `append-to-buffer' command is very nearly as simple as the
  497. `mark-whole-buffer' command.  What it does is copy the region (that is,
  498. the part of the buffer between point and mark) from the current buffer
  499. to a specified buffer.
  500.  
  501.    The `append-to-buffer' command uses the `insert-buffer-substring'
  502. function to copy the region.  `insert-buffer-substring' is described by
  503. its name: it takes a string of characters from part of a buffer, a
  504. "substring", and inserts them into another buffer.  Most of
  505. `append-to-buffer' is concerned with setting up the conditions for
  506. `insert-buffer-substring' to work: the code must specify both the
  507. buffer to which the text will go and the region that will be copied.
  508. Here is the complete text of the function:
  509.  
  510.      (defun append-to-buffer (buffer start end)
  511.        "Append to specified buffer the text of the region.
  512.      It is inserted into that buffer before its point.
  513.      
  514.      When calling from a program, give three arguments:
  515.      a buffer or the name of one, and two character numbers
  516.      specifying the portion of the current buffer to be copied."
  517.        (interactive "BAppend to buffer: \nr")
  518.        (let ((oldbuf (current-buffer)))
  519.          (save-excursion
  520.            (set-buffer (get-buffer-create buffer))
  521.            (insert-buffer-substring oldbuf start end))))
  522.  
  523.    The function can be understood by looking at it as a series of
  524. filled-in templates.
  525.  
  526.    The outermost template is for the function definition.  In this case,
  527. it looks like this (with several slots filled in):
  528.  
  529.      (defun append-to-buffer (buffer start end)
  530.        "DOCUMENTATION..."
  531.        (interactive "BAppend to buffer: \nr")
  532.        BODY...)
  533.  
  534.    The first line of the function includes its name and three arguments.
  535. The arguments are the `buffer' to which the text will be copied, and
  536. the `start' and `end' of the region in the current buffer that will be
  537. copied.
  538.  
  539.    The next part of the function is the documentation, which is clear
  540. and complete.
  541.  
  542. * Menu:
  543.  
  544. * append interactive::          A two part interactive expression.
  545. * append-to-buffer body::       Incorporates a `let' expression.
  546. * append save-excursion::       How the `save-excursion' works.
  547.  
  548. 
  549. File: emacs-lisp-intro.info,  Node: append interactive,  Next: append-to-buffer body,  Prev: append-to-buffer,  Up: append-to-buffer
  550.  
  551. The `append-to-buffer' Interactive Expression
  552. ---------------------------------------------
  553.  
  554.    Since the `append-to-buffer' function will be used interactively,
  555. the function must have an `interactive' expression.  (For a review of
  556. `interactive', see *Note Making a Function Interactive: Interactive.)
  557. The expression reads as follows:
  558.  
  559.      (interactive "BAppend to buffer: \nr")
  560.  
  561. This expression has an argument inside of quotation marks and that
  562. argument has two parts, separated by `\n'.
  563.  
  564.    The first part is `BAppend to buffer: '.  Here, the `B' tells Emacs
  565. to ask for the name of the buffer that will be passed to the function.
  566. Emacs will ask for the name by prompting the user in the minibuffer,
  567. using the string following the `B', which is the string `Append to
  568. buffer: '.  Emacs then binds the variable `buffer' in the function's
  569. argument list to the specified buffer.
  570.  
  571.    The newline, `\n', separates the first part of the argument from the
  572. second part.  It is followed by an `r' that tells Emacs to bind the two
  573. arguments that follow the symbol `buffer' in the function's argument
  574. list (that is, `start' and `end') to the values of point and mark.
  575.  
  576. 
  577. File: emacs-lisp-intro.info,  Node: append-to-buffer body,  Next: append save-excursion,  Prev: append interactive,  Up: append-to-buffer
  578.  
  579. The Body of `append-to-buffer'
  580. ------------------------------
  581.  
  582.    The body of the `append-to-buffer' function begins with `let'.
  583.  
  584.    As we have seen before (*note `let': let.), the purpose of a `let'
  585. expression is to create and give initial values to one or more
  586. variables that will only be used within the body of the `let'.  This
  587. means that such a variable will not be confused with any variable of
  588. the same name outside the `let' expression.
  589.  
  590.    We can see how the `let' expression fits into the function as a
  591. whole by showing a template for `append-to-buffer' with the `let'
  592. expression in outline:
  593.  
  594.      (defun append-to-buffer (buffer start end)
  595.        "DOCUMENTATION..."
  596.        (interactive "BAppend to buffer: \nr")
  597.        (let ((VARIABLE VALUE))
  598.              BODY...)
  599.  
  600.    The `let' expression has three elements:
  601.  
  602.   1. The symbol `let';
  603.  
  604.   2. A varlist containing, in this case, a single two-element list,
  605.      `(VARIABLE VALUE)';
  606.  
  607.   3. The body of the `let' expression.
  608.  
  609.    In the `append-to-buffer' function, the varlist looks like this:
  610.  
  611.      (oldbuf (current-buffer))
  612.  
  613. In this part of the `let' expression, the one variable, `oldbuf', is
  614. bound to the value returned by the `(current-buffer)' expression.  The
  615. variable, `oldbuf', is used to keep track of the buffer in which you
  616. are working.
  617.  
  618.    The element or elements of a varlist are surrounded by a set of
  619. parentheses so the Lisp interpreter can distinguish the varlist from
  620. the body of the `let'.  As a consequence, the two-element list within
  621. the varlist is surrounded by a circumscribing set of parentheses.  The
  622. line looks like this:
  623.  
  624.      (let ((oldbuf (current-buffer)))
  625.        ... )
  626.  
  627. The two parentheses before `oldbuf' might surprise you if you did not
  628. realize that the first parenthesis before `oldbuf' marks the boundary
  629. of the varlist and the second parenthesis marks the beginning of the
  630. two-element list, `(oldbuf (current-buffer))'.
  631.  
  632. 
  633. File: emacs-lisp-intro.info,  Node: append save-excursion,  Prev: append-to-buffer body,  Up: append-to-buffer
  634.  
  635. `save-excursion' in `append-to-buffer'
  636. --------------------------------------
  637.  
  638.    The body of the `let' expression in `append-to-buffer' consists of a
  639. `save-excursion' expression.
  640.  
  641.    The `save-excursion' function saves the locations of point and mark,
  642. and restores them to those positions after the expressions in the body
  643. of the `save-excursion' complete execution.  In addition,
  644. `save-excursion' keeps track of the original buffer, and restores it.
  645. This is how `save-excursion' is used in `append-to-buffer'.
  646.  
  647.    Incidentally, it is worth noting here that a Lisp function is
  648. normally formatted so that everything that is enclosed in a multi-line
  649. spread is indented more to the right than the first symbol.  In this
  650. function definition, the `let' is indented more than the `defun', and
  651. the `save-excursion' is indented more than the `let', like this:
  652.  
  653.      (defun ...
  654.        ...
  655.        ...
  656.        (let...
  657.          (save-excursion
  658.            ...
  659.  
  660. This formatting convention makes it easy to see that the two lines in
  661. the body of the `save-excursion' are enclosed by the parentheses
  662. associated with `save-excursion', just as the `save-excursion' itself
  663. is enclosed by the parentheses associated with the `let':
  664.  
  665.      (let ((oldbuf (current-buffer)))
  666.        (save-excursion
  667.          (set-buffer (get-buffer-create buffer))
  668.          (insert-buffer-substring oldbuf start end))))
  669.  
  670.    The use of the `save-excursion' function can be viewed as a process
  671. of filling in the slots of a template:
  672.  
  673.      (save-excursion
  674.        FIRST-EXPRESSION-IN-BODY
  675.        SECOND-EXPRESSION-IN-BODY
  676.         ...
  677.        LAST-EXPRESSION-IN-BODY)
  678.  
  679. In this function, the body of the `save-excursion' contains only two
  680. expressions.  The body looks like this:
  681.  
  682.      (set-buffer (get-buffer-create buffer))
  683.      (insert-buffer-substring oldbuf start end)
  684.  
  685.    When the `append-to-buffer' function is evaluated, the two
  686. expressions in the body of the `save-excursion' are evaluated in
  687. sequence.  The value of the last expression is returned as the value of
  688. the `save-excursion' function; the other expression is evaluated only
  689. for its side effects.
  690.  
  691.    The first line in the body of the `save-excursion' uses the
  692. `set-buffer' function to change the current buffer to the one specified
  693. in the first argument to `append-to-buffer'.  (Changing the buffer is
  694. the side effect; as we have said before, in Lisp, a side effect is
  695. often the primary thing we want.)  The second line does the primary
  696. work of the function.
  697.  
  698.    The `set-buffer' function changes Emacs' attention to the buffer to
  699. which the text will be copied and from which `save-excursion' will
  700. return.  The line looks like this:
  701.  
  702.      (set-buffer (get-buffer-create buffer))
  703.  
  704.    The innermost expression of this list is `(get-buffer-create
  705. buffer)'.  This expression uses the `get-buffer-create' function, which
  706. either gets the named buffer, or if it does not exist, creates one with
  707. the given name.  This means you can use `append-to-buffer' to put text
  708. into a buffer that did not previously exist.
  709.  
  710.    `get-buffer-create' also keeps `set-buffer' from getting an
  711. unnecessary error: `set-buffer' needs a buffer to go to; if you were to
  712. specify a buffer that does not exist, Emacs would baulk.  Since
  713. `get-buffer-create' will create a buffer if none exists, `set-buffer'
  714. is always provided with a buffer.
  715.  
  716.    The last line of `append-to-buffer' does the work of appending the
  717. text:
  718.  
  719.      (insert-buffer-substring oldbuf start end)
  720.  
  721. The `insert-buffer-substring' function copies a string *from* the
  722. buffer specified as its first argument and inserts the string into the
  723. present buffer.  In this case, the argument to
  724. `insert-buffer-substring' is the value of the variable created and
  725. bound by the `let', namely the value of `oldbuf', which was the current
  726. buffer when you gave the `append-to-buffer' command.
  727.  
  728.    After `insert-buffer-substring' has done its work, `save-excursion'
  729. will restore the action to the original buffer and `append-to-buffer'
  730. will have done its job.
  731.  
  732.    Written in skeletal form, the workings of the body look like this:
  733.  
  734.      (let (BIND-`oldbuf'-TO-VALUE-OF-`current-buffer')
  735.        (save-excursion                       ; Keep track of buffer.
  736.          CHANGE-BUFFER
  737.          INSERT-SUBSTRING-FROM-`oldbuf'-INTO-BUFFER)
  738.      
  739.        CHANGE-BACK-TO-ORIGINAL-BUFFER-WHEN-FINISHED
  740.      LET-THE-LOCAL-MEANING-OF-`oldbuf'-DISAPPEAR-WHEN-FINISHED
  741.  
  742.    In summary, `append-to-buffer' works as follows: it saves the value
  743. of the current buffer in the variable called `oldbuf'.  It gets the new
  744. buffer, creating one if need be, and switches Emacs to it.  Using the
  745. value of `oldbuf', it inserts the region of text from the old buffer
  746. into the new buffer; and then using `save-excursion', it brings you
  747. back to your original buffer.
  748.  
  749.    In looking at `append-to-buffer', you have explored a fairly complex
  750. function.  It shows how to use `let' and `save-excursion', and how to
  751. change to and come back from another buffer.  Many function definitions
  752. use `let', `save-excursion', and `set-buffer' this way.
  753.  
  754. 
  755. File: emacs-lisp-intro.info,  Node: Buffer Related Review,  Next: Buffer Exercises,  Prev: append-to-buffer,  Up: Buffer Walk Through
  756.  
  757. Review
  758. ======
  759.  
  760.    Here is a brief summary of the various functions discussed in this
  761. chapter.
  762.  
  763. `describe-function'
  764. `describe-variable'
  765.      Print the documentation for a function or variable.
  766.      Conventionally bound to `C-h f' and `C-h v'.
  767.  
  768. `find-tag'
  769.      Find the file containing the source for a function or variable and
  770.      switch buffers to it, positioning point at the beginning of the
  771.      item.  Conventionally bound to `M-.' (that's a period following the
  772.      <META> key).
  773.  
  774. `save-excursion'
  775.      Save the location of point and mark and restore their values after
  776.      the arguments to `save-excursion' have been evaluated.  Also,
  777.      remember the current buffer and return to it.
  778.  
  779. `push-mark'
  780.      Set mark at a location and record the value of the previous mark
  781.      on the mark ring.  The mark is a location in the buffer that will
  782.      keep its relative position even if text is added to or removed
  783.      from the buffer.
  784.  
  785. `goto-char'
  786.      Set point to the location specified by the value of the argument,
  787.      which can be a number, a marker,  or an expression that returns
  788.      the number of a position, such as `(point-min)'.
  789.  
  790. `insert-buffer-substring'
  791.      Copy a region of text from a buffer that is passed to the function
  792.      as an argument and insert the region into the current buffer.
  793.  
  794. `mark-whole-buffer'
  795.      Mark the whole buffer as a region.  Normally bound to `C-x h'.
  796.  
  797. `set-buffer'
  798.      Switch the attention of Emacs to another buffer, but do not change
  799.      the window being displayed.  Used when the program rather than a
  800.      human is to work on a different buffer.
  801.  
  802. `get-buffer-create'
  803. `get-buffer'
  804.      Find a named buffer or create one if a buffer of that name does not
  805.      exist.  The `get-buffer' function returns `nil' if the named
  806.      buffer does not exist.
  807.  
  808. 
  809. File: emacs-lisp-intro.info,  Node: Buffer Exercises,  Prev: Buffer Related Review,  Up: Buffer Walk Through
  810.  
  811. Exercises
  812. =========
  813.  
  814.    * Write your own `simplified-end-of-buffer' function definition;
  815.      then test it to see whether it works.
  816.  
  817.    * Use `if' and `get-buffer' to write a function that prints a
  818.      message telling you whether a buffer exists.
  819.  
  820.    * Using `find-tag', find the source for the `copy-to-buffer'
  821.      function.
  822.  
  823. 
  824. File: emacs-lisp-intro.info,  Node: More Complex,  Next: Narrowing & Widening,  Prev: Buffer Walk Through,  Up: Top
  825.  
  826. A Few More Complex Functions
  827. ****************************
  828.  
  829.    In this chapter, we build on what we have learned in previous
  830. chapters by looking at more complex functions.  The `copy-to-buffer'
  831. function illustrates use of two `save-excursion' expressions in one
  832. definition, while the `insert-buffer' function illustrates use of <*>
  833. in an `interactive' expression, use of `or', and the important
  834. distinction between a name and the object to which the name refers.
  835.  
  836. * Menu:
  837.  
  838. * copy-to-buffer::              With `set-buffer', `get-buffer-create'.
  839. * insert-buffer::               Read-only, and with `or'.
  840. * beginning-of-buffer::         Shows `goto-char',
  841.                                 `point-min', and `push-mark'.
  842. * Second Buffer Related Review::
  843. * &optional Exercise ::
  844.  
  845. 
  846. File: emacs-lisp-intro.info,  Node: copy-to-buffer,  Next: insert-buffer,  Prev: More Complex,  Up: More Complex
  847.  
  848. The Definition of `copy-to-buffer'
  849. ==================================
  850.  
  851.    After understanding how `append-to-buffer' works, it is easy to
  852. understand `copy-to-buffer'.  This function copies text into a buffer,
  853. but instead of adding to the second buffer, it replaces the previous
  854. text in the second buffer.  The code for the `copy-to-buffer' function
  855. is almost the same as the code for `append-to-buffer', except that
  856. `erase-buffer' and a second `save-excursion' are used.  (*Note The
  857. Definition of `append-to-buffer': append-to-buffer, for the description
  858. of `append-to-buffer'.)
  859.  
  860.    The body of `copy-to-buffer' looks like this
  861.  
  862.      ...
  863.      (interactive "BCopy to buffer: \nr")
  864.        (let ((oldbuf (current-buffer)))
  865.          (save-excursion
  866.            (set-buffer (get-buffer-create buffer))
  867.            (erase-buffer)
  868.            (save-excursion
  869.              (insert-buffer-substring oldbuf start end)))))
  870.  
  871.    This code is similar to the code in `append-to-buffer': it is only
  872. after changing to the buffer to which the text will be copied that the
  873. definition for this function diverges from the definition for
  874. `append-to-buffer': the `copy-to-buffer' function erases the buffer's
  875. former contents.  (This is what is meant by `replacement'; to replace
  876. text, Emacs erases the previous text and then inserts new text.)  After
  877. erasing the previous contents of the buffer, `save-excursion' is used
  878. for a second time and the new text is inserted.
  879.  
  880.    Why is `save-excursion' used twice?  Consider again what the
  881. function does.
  882.  
  883.    In outline, the body of `copy-to-buffer' looks like this:
  884.  
  885.      (let (BIND-`oldbuf'-TO-VALUE-OF-`current-buffer')
  886.        (save-excursion         ; First use of `save-excursion'.
  887.          CHANGE-BUFFER
  888.            (erase-buffer)
  889.            (save-excursion     ; Second use of `save-excursion'.
  890.              INSERT-SUBSTRING-FROM-`oldbuf'-INTO-BUFFER)))
  891.  
  892.    The first use of `save-excursion' returns Emacs to the buffer from
  893. which the text is being copied.  That is clear, and is just like its use
  894. in `append-to-buffer'.  Why the second use?  The reason is that
  895. `insert-buffer-substring' always leaves point at the *end* of the
  896. region being inserted.  The second `save-excursion' causes Emacs to
  897. leave point at the beginning of the text being inserted.  In most
  898. circumstances, users prefer to find point at the beginning of inserted
  899. text.  (Of course, the `copy-to-buffer' function returns the user to
  900. the original buffer when done--but if the user *then* switches to the
  901. copied-to buffer, point will go to the beginning of the text.  Thus,
  902. this use of a second `save-excursion' is a little nicety.)
  903.  
  904. 
  905. File: emacs-lisp-intro.info,  Node: insert-buffer,  Next: beginning-of-buffer,  Prev: copy-to-buffer,  Up: More Complex
  906.  
  907. The Definition of `insert-buffer'
  908. =================================
  909.  
  910.    `insert-buffer' is yet another buffer-related function.  This
  911. command copies another buffer *into* the current buffer.  It is the
  912. reverse of `append-to-buffer' or `copy-to-buffer', since they copy a
  913. region of text *from* the current buffer to another buffer.
  914.  
  915.    In addition, this code illustrates the use of `interactive' with a
  916. buffer that might be "read-only" and the important distinction between
  917. the name of an object and the object actually referred to.  Here is the
  918. code:
  919.  
  920.      (defun insert-buffer (buffer)
  921.        "Insert after point the contents of BUFFER.
  922.      Puts mark after the inserted text.
  923.      BUFFER may be a buffer or a buffer name."
  924.        (interactive "*bInsert buffer: ")
  925.        (or (bufferp buffer)
  926.            (setq buffer (get-buffer buffer)))
  927.        (let (start end newmark)
  928.          (save-excursion
  929.            (save-excursion
  930.              (set-buffer buffer)
  931.              (setq start (point-min) end (point-max)))
  932.            (insert-buffer-substring buffer start end)
  933.            (setq newmark (point)))
  934.          (push-mark newmark)))
  935.  
  936.    As with other function definitions, you can use a template to see an
  937. outline of the function:
  938.  
  939.      (defun insert-buffer (buffer)
  940.        "DOCUMENTATION..."
  941.        (interactive "*bInsert buffer: ")
  942.        BODY...)
  943.  
  944. * Menu:
  945.  
  946. * insert interactive expression::  A read-only situation.
  947. * insert-buffer body::          The body has an `or' and a ` let'.
  948. * if & or::                     Using an `if' instead of an `or'.
  949. * insert or::                   How the  `or' expression works.
  950. * insert let::                  Two `save-excursion' expressions.
  951.  
  952. 
  953. File: emacs-lisp-intro.info,  Node: insert interactive expression,  Next: insert-buffer body,  Prev: insert-buffer,  Up: insert-buffer
  954.  
  955. The Interactive Expression in `insert-buffer'
  956. ---------------------------------------------
  957.  
  958.    In `insert-buffer', the argument to the `interactive' declaration
  959. has two parts, an asterisk, `*', and `bInsert buffer: '.
  960.  
  961. * Menu:
  962.  
  963. * read-only buffer::
  964. * b for interactive::
  965.  
  966. 
  967. File: emacs-lisp-intro.info,  Node: read-only buffer,  Next: b for interactive,  Prev: insert interactive expression,  Up: insert interactive expression
  968.  
  969. A Read-only Buffer
  970. ..................
  971.  
  972.    The asterisk is for the situation when the buffer is a read-only
  973. buffer--a buffer that cannot be modified.  If `insert-buffer' is called
  974. on a buffer that is read-only, a message to this effect is printed in
  975. the echo area and the terminal may beep or blink at you; you will not
  976. be permitted to insert anything into current buffer.  The asterisk does
  977. not need to be followed by a newline to separate it from the next
  978. argument.
  979.  
  980. 
  981. File: emacs-lisp-intro.info,  Node: b for interactive,  Prev: read-only buffer,  Up: insert interactive expression
  982.  
  983. `b' in an Interactive Expression
  984. ................................
  985.  
  986.    The next argument in the interactive expression starts with a lower
  987. case `b'.  (This is different from the code for `append-to-buffer',
  988. which uses an upper-case `B'.  *Note The Definition of
  989. `append-to-buffer': append-to-buffer.) The lower-case `b' tells the
  990. Lisp interpreter that the argument for `insert-buffer' should be an
  991. existing buffer or else its name.  (The upper-case `B' option provides
  992. for the possibility that the buffer does not exist.)  Emacs will prompt
  993. you for the name of the buffer, offering you a default buffer, with
  994. name completion enabled.  If the buffer does not exist, you receive a
  995. message that says "No match"; your terminal may beep at you as well.
  996.  
  997. 
  998. File: emacs-lisp-intro.info,  Node: insert-buffer body,  Next: if & or,  Prev: insert interactive expression,  Up: insert-buffer
  999.  
  1000. The Body of the `insert-buffer' Function
  1001. ----------------------------------------
  1002.  
  1003.    The body of the `insert-buffer' function has two major parts: an
  1004. `or' expression and a `let' expression.  The purpose of the `or'
  1005. expression is to ensure that the argument `buffer' is bound to a buffer
  1006. and not just the name of a buffer.  The body of the `let' expression
  1007. contains the code which copies the other buffer into the current buffer.
  1008.  
  1009.    In outline, the two expressions fit into the `insert-buffer'
  1010. function like this:
  1011.  
  1012.      (defun insert-buffer (buffer)
  1013.        "DOCUMENTATION..."
  1014.        (interactive "*bInsert buffer: ")
  1015.        (or ...
  1016.            ...
  1017.        (let (VARLIST)
  1018.            BODY-OF-`let'... )
  1019.  
  1020.    To understand how the `or' expression ensures that the argument
  1021. `buffer' is bound to a buffer and not to the name of a buffer, it is
  1022. first necessary to understand the `or' function.
  1023.  
  1024.    Before doing this, let me rewrite this part of the function using
  1025. `if' so that you can see what is done in a manner that will be familiar.
  1026.  
  1027. 
  1028. File: emacs-lisp-intro.info,  Node: if & or,  Next: insert or,  Prev: insert-buffer body,  Up: insert-buffer
  1029.  
  1030. `insert-buffer' With an `if' Instead of an `or'
  1031. -----------------------------------------------
  1032.  
  1033.    The job to be done is to make sure the value of `buffer' is a buffer
  1034. itself and not the name of a buffer.  If the value is the name, then
  1035. the buffer itself must be got.
  1036.  
  1037.    You can imagine yourself at a conference where an usher is wandering
  1038. around holding a list with your name on it and looking for you: the
  1039. usher is "bound" to your name, not to you; but when the usher finds you
  1040. and takes your arm, the usher becomes "bound" to you.
  1041.  
  1042.    In Lisp, you might describe this situation like this:
  1043.  
  1044.      (if (not (holding-on-to-guest))
  1045.          (find-and-take-arm-of-guest))
  1046.  
  1047.    We want to do the same thing with a buffer--if we do not have the
  1048. buffer itself, we want to get it.
  1049.  
  1050.    Using a predicate called `bufferp' that tells us whether we have a
  1051. buffer (rather than its name), we can write the code like this:
  1052.  
  1053.      (if (not (bufferp buffer))              ; if-part
  1054.          (setq buffer (get-buffer buffer)))  ; then-part
  1055.  
  1056. Here, the true-or-false-test of the `if' expression is
  1057. `(not (bufferp buffer))'; and the then-part is the expression
  1058. `(setq buffer (get-buffer buffer))'.
  1059.  
  1060.    In the test, the function `bufferp' returns true if its argument is
  1061. a buffer--but false if its argument is the name of the buffer.  (The
  1062. last character of the function name `bufferp' is the character `p'; as
  1063. we saw earlier, such use of `p' is a convention that indicates that the
  1064. function is a predicate, which is a term that means that the function
  1065. will determine whether some property is true or false.  *Note Using the
  1066. Wrong Type Object as an Argument: Wrong Type of Argument.)
  1067.  
  1068.    The function `not' precedes the expression `(bufferp buffer)', so
  1069. the true-or-false-test looks like this:
  1070.  
  1071.      (not (bufferp buffer))
  1072.  
  1073. `not' is a function the returns true if its argument is false and false
  1074. if its argument is true.  So if `(bufferp buffer)' returns true, the
  1075. `not' expression returns false and vice-versa: what is "not true" is
  1076. false and what is "not false" is true.
  1077.  
  1078.    Using this test, the `if' expression works as follows: when the
  1079. value of the variable `buffer' is actually a buffer rather then its
  1080. name, the true-or-false-test returns false and the `if' expression does
  1081. not evaluate the then-part.  This is fine, since we do not need to do
  1082. anything to the variable `buffer' if it really is a buffer.
  1083.  
  1084.    On the other hand, when the value of `buffer' is not a buffer
  1085. itself, but the name of a buffer, the true-or-false-test returns true
  1086. and the then-part of the expression is evaluated.  In this case, the
  1087. then-part is `(setq buffer (get-buffer buffer))'.  This expression uses
  1088. the `get-buffer' function to return an actual buffer itself, given its
  1089. name.  The `setq' then sets the variable `buffer' to the value of the
  1090. buffer itself, replacing its previous value (which was the name of the
  1091. buffer).
  1092.  
  1093. 
  1094. File: emacs-lisp-intro.info,  Node: insert or,  Next: insert let,  Prev: if & or,  Up: insert-buffer
  1095.  
  1096. The `or' in the Body
  1097. --------------------
  1098.  
  1099.    The purpose of the `or' expression in the `insert-buffer' function
  1100. is to ensure that the argument `buffer' is bound to a buffer and not
  1101. just the name of a buffer.  The previous section shows how the job
  1102. could have been done using an `if' expression.  However, the
  1103. `insert-buffer' function actually uses `or'.  To understand this, it is
  1104. necessary to understand how `or' works.
  1105.  
  1106.    An `or' function can have any number of arguments.  It evaluates
  1107. each argument in turn and returns the value of the first of its
  1108. arguments that is not `nil'.  Also, and this is a crucial feature of
  1109. `or', it does not evaluate any subsequent arguments after returning the
  1110. first non-`nil' value.
  1111.  
  1112.    The `or' expression looks like this:
  1113.  
  1114.      (or (bufferp buffer)
  1115.          (setq buffer (get-buffer buffer)))
  1116.  
  1117. The first argument to `or' is the expression `(bufferp buffer)'.  This
  1118. expression returns true (a non-`nil' value) if the buffer is actually a
  1119. buffer, and not just the name of a buffer.  In the `or' expression, if
  1120. this is the case, the `or' expression returns this true value and does
  1121. not evaluate the next expression--and this is fine with us, since we do
  1122. not want to do anything to the value of `buffer' if it really is a
  1123. buffer.
  1124.  
  1125.    On the other hand, if the value of `(bufferp buffer)' is `nil',
  1126. which it will be if the value of `buffer' is the name of a buffer, the
  1127. Lisp interpreter evaluates the next element of the `or' expression.
  1128. This is the expression `(setq buffer (get-buffer buffer))'.  This
  1129. expression returns a non-`nil' value, which is the value to which it
  1130. sets the variable `buffer'--and this value is a buffer itself, not the
  1131. name of a buffer.
  1132.  
  1133.    The result of all this is that the symbol `buffer' is always bound
  1134. to a buffer itself rather than the name of a buffer.  All this is
  1135. necessary because the `set-buffer' function in a following line only
  1136. works with a buffer itself, not with the name to a buffer.
  1137.  
  1138.    Incidentally, using `or', the situation with the usher would be
  1139. written like this:
  1140.  
  1141.      (or (holding-on-to-guest) (find-and-take-arm-of-guest))
  1142.  
  1143. 
  1144. File: emacs-lisp-intro.info,  Node: insert let,  Prev: insert or,  Up: insert-buffer
  1145.  
  1146. The `let' Expression in `insert-buffer'
  1147. ---------------------------------------
  1148.  
  1149.    After ensuring that the variable `buffer' refers to a buffer itself
  1150. and not just to the name of a buffer, the `insert-buffer function'
  1151. continues with a `let' expression.  This specifies three local
  1152. variables, `start', `end', and `newmark' and binds them to the initial
  1153. value `nil'.  These variables are used inside the remainder of the
  1154. `let' and temporarily hide any other occurrence of variables of the
  1155. same name in Emacs until the end of the `let'.
  1156.  
  1157.    The body of the `let' contains two `save-excursion' expressions.
  1158. First, we will look at the inner `save-excursion' expression in detail.
  1159. The expression looks like this:
  1160.  
  1161.      (save-excursion
  1162.        (set-buffer buffer)
  1163.        (setq start (point-min) end (point-max)))
  1164.  
  1165. The expression `(set-buffer buffer)' changes Emacs's attention from the
  1166. current buffer to the one from which the text will copied.  In that
  1167. buffer, the variables `start' and `end' are set to the beginning and
  1168. end of the buffer, using the commands `point-min' and `point-max'.
  1169. Note that we have here an illustration of how `setq' is able to set two
  1170. variables in the same expression.  `setq''s first argument is set to
  1171. the value of its second and its third argument is set to the value of
  1172. its fourth.
  1173.  
  1174.    After the body of the inner `save-excursion' is evaluated, the
  1175. `save-excursion' restores the original buffer, but `start' and `end'
  1176. remain set to the values of the beginning and end of the buffer from
  1177. which the text will be copied.
  1178.  
  1179.    The outer `save-excursion' expression looks like this:
  1180.  
  1181.      (save-excursion
  1182.        (INNER-`save-excursion'-EXPRESSION
  1183.           (GO-TO-NEW-BUFFER-AND-SET-`start'-AND-`end')
  1184.        (insert-buffer-substring buffer start end)
  1185.        (setq newmark (point)))
  1186.  
  1187. The `insert-buffer-substring' function copies the text *into* the
  1188. current buffer *from* the region indicated by `start' and `end' in
  1189. `buffer'.  Since the whole of the second buffer lies between `start'
  1190. and `end', the whole of the second buffer is copied into the buffer you
  1191. are editing.  Next, the value of point, which will be at the end of the
  1192. inserted text, is recorded in the variable `newmark'.
  1193.  
  1194.    After the body of the outer `save-excursion' is evaluated, point and
  1195. mark are relocated to their original places.
  1196.  
  1197.    However, it is convenient to locate a mark at the end of the newly
  1198. inserted text and locate point at its beginning.  The `newmark'
  1199. variable records the end of the inserted text.  In the last line of the
  1200. `let' expression, the `(push-mark newmark)' expression function sets a
  1201. mark to this location.  (The previous location of the mark is still
  1202. accessible; it is recorded on the mark ring and you can go back to it
  1203. with `C-u C-<SPC>'.)  Meanwhile, point is located at the beginning of
  1204. the inserted text, which is where it was before you called the insert
  1205. function.
  1206.  
  1207.    The whole `let' expression looks like this:
  1208.  
  1209.      (let (start end newmark)
  1210.        (save-excursion
  1211.          (save-excursion
  1212.            (set-buffer buffer)
  1213.            (setq start (point-min) end (point-max)))
  1214.          (insert-buffer-substring buffer start end)
  1215.          (setq newmark (point)))
  1216.        (push-mark newmark))
  1217.  
  1218.    Like the `append-to-buffer' function, the `insert-buffer' function
  1219. uses `let', `save-excursion', and `set-buffer'.  In addition, the
  1220. function illustrates one way to use `or'.  All these functions are
  1221. building blocks that we will find and use again and again.
  1222.  
  1223.